package com.qimo.zzy.service;

import com.aliyun.credentials.utils.StringUtils;
import com.qimo.zzy.entity.ResultEntity;
import com.qimo.zzy.tool.JSONTool;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static com.qimo.zzy.tool.Email.VerCodeGenerateTool.generateRandomString;

@Service
public class MailService {
    private static final Logger logger = LoggerFactory.getLogger(MailService.class);//日志。getLogger需要传入当前的类名
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private JavaMailSenderImpl javaMailSender;

    @Value("${spring.mail.username}")
    private String sendMailer;

    /**
     * 检测邮件信息类
     *
     * @param to
     * @param subject
     * @param text
     */
    private void checkMail(String to, String subject, String text) {
        if (StringUtils.isEmpty(to)) {
            throw new RuntimeException("邮件收信人不能为空");
        }
        if (StringUtils.isEmpty(subject)) {
            throw new RuntimeException("邮件主题不能为空");
        }
        if (StringUtils.isEmpty(text)) {
            throw new RuntimeException("邮件内容不能为空");
        }
    }

    /**
     * 发送纯文本邮件
     *
     * @param to
     * @param subject
     * @param text
     */
    public String sendTextMailMessage(String to, String subject, String text) {
        ResultEntity resultEntity = new ResultEntity(null, null, null);
        try {
            //true 代表支持复杂的类型
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true);
            //邮件发信人
            mimeMessageHelper.setFrom(sendMailer);
            //邮件收信人  1或多个
            mimeMessageHelper.setTo(to.split(","));
            //邮件主题
            mimeMessageHelper.setSubject(subject);
            //邮件内容
            mimeMessageHelper.setText(text);
            //邮件发送时间
            mimeMessageHelper.setSentDate(new Date());
            //发送邮件
            javaMailSender.send(mimeMessageHelper.getMimeMessage());
            resultEntity.setCode(200);
            resultEntity.setMsg("发送成功");

        } catch (MessagingException e) {
            e.printStackTrace();
            resultEntity.setCode(900);
            resultEntity.setMsg("发送失败");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    /**
     * 有邮件发送图片
     *
     * @param to
     * @param subject
     * @param text
     * @return
     */
    public String sendPhotoMailMessage(String to, String subject, String text) {
        ResultEntity resultEntity = new ResultEntity(null, null, null);
        try {
            //true 代表支持复杂的类型
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true);
            //邮件发信人
            mimeMessageHelper.setFrom(sendMailer);
            //邮件收信人  1或多个
            mimeMessageHelper.setTo(to.split(","));
            //邮件主题
            mimeMessageHelper.setSubject(subject);
            //邮件内容
            mimeMessageHelper.setText(text);
            //邮件发送时间
            mimeMessageHelper.setSentDate(new Date());
            String imgId = "1";
            String content = "<html><body>" + text + "<img src=\'cid:" + imgId + "\'></img>" + "</body></html>";

            mimeMessageHelper.setText(content, true);

            String imgPath = "C:\\Users\\20654\\Pictures\\原神\\shenzi.jpg";

            File file = new File(imgPath);

            FileSystemResource resource = new FileSystemResource(file);
            mimeMessageHelper.addInline(imgId, resource);
            //发送邮件
            javaMailSender.send(mimeMessageHelper.getMimeMessage());
            resultEntity.setCode(200);
            resultEntity.setMsg("发送成功");

        } catch (MessagingException e) {
            e.printStackTrace();
            resultEntity.setCode(900);
            resultEntity.setMsg("发送失败");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    /**
     * 发送随机验证码
     *
     * @param to
     * @return
     */
    public String sendRoundTextMailMessage(String to) {
        ResultEntity resultEntity = new ResultEntity(null, null, null);
        try {
            //true 代表支持复杂的类型
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true);
            //邮件发信人
            mimeMessageHelper.setFrom(sendMailer);
            //邮件收信人  1或多个
            mimeMessageHelper.setTo(to.split(","));
            //邮件主题
//            mimeMessageHelper.setSubject(subject);
            String code = generateRandomString();
            //生成随机的验证码
            String text = "【多余大师】验证码" +  code + "，您正在进行身份验证，该验证码10分钟内有效，请勿泄露于他人。";
            //邮件内容
            mimeMessageHelper.setText(text);
            //邮件发送时间
            mimeMessageHelper.setSentDate(new Date());
            //发送邮件
            javaMailSender.send(mimeMessageHelper.getMimeMessage());
            //将验证码缓存到redis
            stringRedisTemplate.opsForValue().set(to, code, 1, TimeUnit.MINUTES);
            System.out.println(stringRedisTemplate.opsForValue().get(to));
            resultEntity.setCode(200);
            resultEntity.setMsg("发送成功");

        } catch (MessagingException e) {
            e.printStackTrace();
            resultEntity.setCode(900);
            resultEntity.setMsg("发送失败");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }


}
